const app = getApp()
var api = require("../../../utils/mr_xu/api_url.js").api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    advice_fee: 0,
    fee: '',
    id: '',
    location: '',
    locationCoords: '',
    destinationCoords: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (!options.id || !options.destinationCoords) {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      })
      wx.navigateBack({
        delta: 1
      })
    }

    this.setData({
      id: options.id,
      destinationCoords: options.destinationCoords
    })
  },
  editFee(e) {
    this.setData({
      fee: e.detail.value
    })
  },
  chooseLocation() {
    let that = this
    wx.chooseLocation({
      success: function(res) {
        if (res.errMsg == 'chooseLocation:ok') {
          console.log(res)
          that.setData({
            location: res.address,
            locationCoords: res.longitude + ',' + res.latitude
          })
          wx.showLoading({
            title: '加载中',
          })
          app.http.post(api.apply_more_money_advise, {
            data: {
              locationCoords: that.data.locationCoords,
              destinationCoords: that.data.destinationCoords
            }
          }).then(res => {
            that.setData({
              advice_fee: res.obj
            })
            wx.hideLoading()
          })
        }
      },
    })
  },
  next() {
    let that = this
    if (this.data.location) {
      wx.showLoading({
        title: '加载中',
      })
      app.http.post(api.apply_more_money, {
        data: {
          difference: parseInt(that.data.fee),
          id: that.data.id,
          locationCoords: that.data.locationCoords,
          location: that.data.location
        }
      }).then(res => {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        wx.hideLoading()
        wx.navigateBack({
          delta: 1
        })
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})